package com.shunda.ysdgj.manager.service.impl.goods;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseService;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.toolkit.JoinWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.shunda.ysdgj.common.exception.CustomException;
import com.shunda.ysdgj.common.util.ValidateUtil;
import com.shunda.ysdgj.manager.mapper.goods.GoodsBrandMapper;
import com.shunda.ysdgj.manager.service.goods.GoodsBrandService;
import com.shunda.ysdgj.model.dto.base.IdsDto;
import com.shunda.ysdgj.model.dto.goods.GoodsBrandFindDto;
import com.shunda.ysdgj.model.entity.goods.GoodsBrand;
import com.shunda.ysdgj.model.vo.ResultCodeEnum;
import com.shunda.ysdgj.model.vo.goods.GoodsBrandVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class GoodsBrandServiceImpl
        extends MPJBaseServiceImpl<GoodsBrandMapper, GoodsBrand>
        implements GoodsBrandService, MPJBaseService<GoodsBrand> {

    public List<GoodsBrandVo> findAll() {
        MPJLambdaWrapper<GoodsBrand> baseWrapper = JoinWrappers
                .lambda(GoodsBrand.class)
                .select(GoodsBrand::getId, GoodsBrand::getBrandName);
        return getBaseMapper().selectJoinList(GoodsBrandVo.class, baseWrapper);
    }

    public IPage<GoodsBrand> findByPage(GoodsBrandFindDto goodsBrandFindDto) {
        if (ValidateUtil.isNotLong(goodsBrandFindDto.getCurrent())) {
            goodsBrandFindDto.setCurrent(1L);
        }
        if (ValidateUtil.isNotLong(goodsBrandFindDto.getSize())) {
            goodsBrandFindDto.setSize(10L);
        }
        LambdaQueryChainWrapper<GoodsBrand> lambdaQuery = lambdaQuery()
                .orderByAsc(GoodsBrand::getBrandSort);
        if (StrUtil.isNotEmpty(goodsBrandFindDto.getBrandName())) {
            lambdaQuery.like(GoodsBrand::getBrandName, goodsBrandFindDto.getBrandName());
        }
        IPage<GoodsBrand> page = new Page<>(goodsBrandFindDto.getCurrent(), goodsBrandFindDto.getSize());
        return lambdaQuery.page(page);
    }

    public boolean add(GoodsBrand goodsBrand) {
        if (ValidateUtil.isBlank(goodsBrand.getBrandName())
                || StrUtil.isEmpty(goodsBrand.getBrandLogo())
                || ValidateUtil.isNotInteger(goodsBrand.getBrandSort())
                || ValidateUtil.isBlank(goodsBrand.getBrandLetter())) {
            throw new CustomException(ResultCodeEnum.PARAM_ERROR);
        }
        Long count = lambdaQuery()
                .eq(GoodsBrand::getBrandName, goodsBrand.getBrandName())
                .count();
        if (count > 0) {
            throw new CustomException(ResultCodeEnum.GOODS_BRAND_REPEAT);
        }
        goodsBrand.setId(null);
        return save(goodsBrand);
    }

    public boolean modify(GoodsBrand goodsBrand) {
        if (ValidateUtil.isNotLong(goodsBrand.getId())
                || ValidateUtil.isBlank(goodsBrand.getBrandName())
                || ValidateUtil.isBlank(goodsBrand.getBrandLogo())
                || ValidateUtil.isNotInteger(goodsBrand.getBrandSort())
                || ValidateUtil.isBlank(goodsBrand.getBrandLetter())) {
            throw new CustomException(ResultCodeEnum.PARAM_ERROR);
        }
        return updateById(goodsBrand);
    }

    @Transactional
    public boolean deleteByIds(IdsDto idsDto) {
        List<Long> ids = idsDto.getIds();
        if (ids.isEmpty()) {
            throw new CustomException(ResultCodeEnum.PARAM_ERROR);
        }
        return removeByIds(ids);
    }

}
